<?php require_once(dirname(__FILE__).'/../include/config.inc.php');
require_once "function.php";

//工单提交处理
$post_time		=time();
$post_time2		=date("Y-m-d H:i:s",time());
 
 // 列出系统中 的接单 任务 
$sql_order = "SELECT id,manageid,post_time,repairid  FROM  `#@__order`  where    jindu='4'   and is_delete=0   order by id desc   ";
$dosql->Execute($sql_order);
while($row = $dosql->GetArray())
{
	$orderid=$row['id'];
	$mids=$row['manageid'];
	$orderidname=date('Ymd',$row['post_time'])."-".$orderid;  //工单号
	$repairname=getRepairMode($row['repairid']);  //任务名称
	//执行人
	$zhixingren="";
	$ids_arr=explode(",",$mids);
	foreach($ids_arr as $key=>$val){
		$zhixingren.=getManage($val)."    ";
	}
	//查找 任务日志
	$row_order=$dosql->GetOne("SELECT *  FROM  `#@__order_log`  where   orderid='$orderid'   AND jindu='4'  order by id desc   ");
	$last_time=$row_order['op_time'];
	
	
	//消息推送
	if($post_time-$last_time>84600){
		//判断是否 推送消息
		$row_timeout=$dosql->GetOne("SELECT *  FROM  `#@__timeout_log`  where   orderid='$orderid'   AND sendmsg='true'  order by id desc   ");
		if(is_array($row_timeout) && isset($row_timeout['id']) ){
				$last_outtime=$row_timeout['op_time'];
				// 判断是否  再次 超过 
				if($post_time-$last_outtime>86400){
					//再次超时 ，记录入库 并发送消息
					foreach($ids_arr as $key=>$val){
						//获取工程师 openid
						$m=$dosql->GetOne("select openid  from `#@__user`  where id='$val'");
						$openid_str	  =$m['openid'];
						//插入更新的状态
					$dosql->ExecNoneQuery("insert into `#@__timeout_log`(orderid,op,op_time,jindu,manageid,sendmsg) values ('$orderid','工单超时提醒','$post_time','4','$val','true')");
					
					$json2='{
						  "touser": "'.$openid_str.'" ,  
						  "template_id": "'.$cfg_order_status.'",    
						  "url":"'.$cfg_weburl.'wx/morderDetail.php?orderid='.$orderid.'",  
						  "data": {
							  "first": {
								  "value": "您好，工单再次超时。请处理", 
								  "color": "#f71b03"
							  }, 
							  "keyword1": {
								  "value": "'.$orderidname.'", 
								  "color": "#173177"
							  }, 
							  "keyword2": {
								  "value": "'.$post_time2.'", 
								  "color": "#173177"
							  },
							  "keyword3": {
								  "value": "'.$zhixingren.'", 
								  "color": "#0e48cb"
							  },
							  "keyword4": {
								  "value": " '.$repairname.'", 
								  "color": "#173177"
							  },
							  "keyword5": {
								  "value": "工单再次超时", 
								  "color": "#173177"
							  },
							  "remark": {
								  "value": "请点击处理，请及时反馈进度。如30分钟后不反馈，系统将扣除奖励1元。服务热线："'.$cfg_hotline.'", 
								  "color": "#f71b03"
							  } 
						  }
						}';
					sendWxMsg($json2);
					
					
					}	
				}
		}else{
			//首次超时 ，记录入库 并发送消息
			
			foreach($ids_arr as $key=>$val){
				//获取工程师 openid
				$m=$dosql->GetOne("select openid  from `#@__user`  where id='$val'");
				$openid_str	  =$m['openid'];
				//插入更新的状态
				$dosql->ExecNoneQuery("insert into `#@__timeout_log`(orderid,op,op_time,jindu,manageid,sendmsg) values ('$orderid','工单超时提醒','$post_time','4','$val','true')");
					$json2='{
						  "touser": "'.$openid_str.'" ,  
						  "template_id": "'.$cfg_order_status.'",    
						  "url":"'.$cfg_weburl.'wx/morderDetail.php?orderid='.$orderid.'",  
						  "data": {
							  "first": {
								  "value": "您好，工单即将超时。请处理", 
								  "color": "#f71b03"
							  }, 
							  "keyword1": {
								  "value": "'.$orderidname.'", 
								  "color": "#173177"
							  }, 
							  "keyword2": {
								  "value": "'.$post_time2.'", 
								  "color": "#173177"
							  },
							  "keyword3": {
								  "value": "'.$zhixingren.'", 
								  "color": "#0e48cb"
							  },
							  "keyword4": {
								  "value": " '.$repairname.'", 
								  "color": "#173177"
							  },
							  "keyword5": {
								  "value": "工单再有30分钟超时", 
								  "color": "#173177"
							  },
							  "remark": {
								   "value": "请点击处理，请及时反馈进度。如30分钟后不反馈，系统将扣除奖励1元。服务热线："'.$cfg_hotline..'", 
								  "color": "#f71b03"
							  } 
						  }
						}';
					sendWxMsg($json2);
					
			}	
		}

	}
	// 扣除奖励 
	if($post_time-$last_time>86400){
		//判断是否 推送消息
		$row_fee=$dosql->GetOne("SELECT *  FROM  `#@__fee_log`  where   orderid='$orderid'   AND op='move'  order by id desc   ");
		if(is_array($row_fee) && isset($row_fee['id']) ){
				$last_feetime=$row_fee['op_time'];
				// 判断是否  再次 超过 
				if($post_time-$last_feetime>86400){
					//再次超时 ，记录入库 并发送消息
					foreach($ids_arr as $key=>$val){
						//获取工程师 openid
						$m=$dosql->GetOne("select openid  from `#@__user`  where id='$val'");
						$openid_str	  =$m['openid'];
						//插入更新的状态
					$dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,fee,remark) values ('$orderid','move','$post_time','4','$val','1','工单超时没有反馈或者处理，扣除奖励1元')");
					 //推送超时提醒

					}	
				}
		}else{
			//首次超时 ，记录入库 并发送消息
			foreach($ids_arr as $key=>$val){
				//获取工程师 openid
				$m=$dosql->GetOne("select openid  from `#@__user`  where id='$val'");
				$openid_str	  =$m['openid'];
				//插入更新的状态
			$dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,fee,remark) values ('$orderid','move','$post_time','4','$val','1','工单超时没有反馈或者处理，扣除奖励1元')");
			}	
		}

	}
	
	
}

?>